package com.crealth.db;

import java.sql.Timestamp;
import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.crealth.Result.DayKpiSignedResult;
import com.crealth.Result.FollowupConsumerResult;
import com.crealth.manager.MybatisManager;
import com.crealth.model.ConsumerProductModel;
import com.crealth.model.LoanContractModel;

public class ConsumerProductDb {	

	public static Integer insertConsumerProduct(ConsumerProductModel product) {
		Integer res = MybatisManager.productMapper.insertConsumerProduct(product);
		MybatisManager.commit();
		
		return res;
	}
	
	public static List<ConsumerProductModel> selectAllConsumerProduct() {
		List<ConsumerProductModel> productls = MybatisManager.productMapper.selectAllConsumerProduct();
		
		return productls;
	}
	
	public static List<ConsumerProductModel> selectConsumerProductByCompanyID(int companyID) {
		List<ConsumerProductModel> productls = MybatisManager.productMapper.selectConsumerProductByCompanyID(companyID);
		
		return productls;
	}
	
	public static List<ConsumerProductModel> selectConsumerProductByMortgagerName(String mortgagerName) {
		List<ConsumerProductModel> productls = MybatisManager.productMapper.selectConsumerProductByMortgagerName(mortgagerName);
		
		return productls;
	}
	
	public static List<ConsumerProductModel> selectConsumerProductByUserID(int userID) {
		List<ConsumerProductModel> productls = MybatisManager.productMapper.selectConsumerProductByUserID(userID);
		
		return productls;
	}
	
	public static ConsumerProductModel selectMainProductByContractID(int contractID) {
		ConsumerProductModel mainProduct = MybatisManager.productMapper.selectMainProductByContractID(contractID);
		
		return mainProduct;
	}
	
	public static List<ConsumerProductModel> selectConsumerProductByUserIDs(String userIDs) {
		List<ConsumerProductModel> productls = MybatisManager.productMapper.selectConsumerProductByUserIDs(userIDs);
		
		return productls;
	}
	
	//  startTm 开始的  签约
	public static List<ConsumerProductModel> selectSignedConsumerProduct(Timestamp weekStartTm, Timestamp weekEndTm) {
		List<ConsumerProductModel> productls = MybatisManager.productMapper.selectSignedConsumerProduct(weekStartTm, weekEndTm);
		
		return productls;
	}
	//  startTm 放款日期
	public static List<ConsumerProductModel> selectExtendLoanConsumerProduct(Timestamp weekStartTm, Timestamp weekEndTm) {
		List<ConsumerProductModel> productls = MybatisManager.productMapper.selectExtendLoanConsumerProduct(weekStartTm, weekEndTm);
		
		return productls;
	}
	//  startTm 收佣日期
	public static List<ConsumerProductModel> selectBrokerageConsumerProduct(Timestamp weekStartTm, Timestamp weekEndTm) {
		List<ConsumerProductModel> productls = MybatisManager.productMapper.selectBrokerageConsumerProduct(weekStartTm, weekEndTm);
		
		return productls;
	}
	
	public static List<ConsumerProductModel> selectSignedExtendLoanProduct(Timestamp startTm, Timestamp endTm) {
		List<ConsumerProductModel> productls = MybatisManager.productMapper.selectSignedExtendLoanProduct(startTm, endTm);
		
		return productls;
	}
			
	
	public static List<Integer> selectContractIDsByMortgagerName(
			@Param("mortgagerName")String mortgagerName) {
		List<Integer> idls = MybatisManager.productMapper.selectContractIDsByMortgagerName(mortgagerName);
		
		return idls;
	}
	
	public static ConsumerProductModel selectConsumerProductByID(int id) {
		ConsumerProductModel product = MybatisManager.productMapper.selectConsumerProductByID(id);
		
		return product;
	}
	
	public static Integer selectContractProductCnt(int contractID) {
		Integer res = MybatisManager.productMapper.selectContractProductCnt(contractID);
		
		return res;
	}
	
	public static Integer updateConsumerProductByID(ConsumerProductModel product) {
		Integer res = MybatisManager.productMapper.updateConsumerProductByID(product);
		MybatisManager.commit();
		
		return res;
	}
	
	public static List<ConsumerProductModel> selectProductByContractID(int contractID) {
		List<ConsumerProductModel> productls = MybatisManager.productMapper.selectProductByContractID(contractID);
		MybatisManager.commit();
		
		return productls;
	}
	
	public static Integer updateConsumerProductAccept(int id, int setAccept) {
		Integer res = MybatisManager.productMapper.updateConsumerProductAccept(id, setAccept);
		MybatisManager.commit();
		
		return res;
	}
	
	public static Integer updateCustomerServiceMemoByID(int id, String csMemo) {
		Integer res = MybatisManager.productMapper.updateCustomerServiceMemoByID(id, csMemo);
		MybatisManager.commit();
		
		return res;
	}
	
	public static Integer updateEarnestStatusByID(int id, int setStatus) {
		Integer res = MybatisManager.productMapper.updateEarnestStatusByID(id, setStatus);
		MybatisManager.commit();
		
		return res;
	}
	
	public static Integer updateProductBrokerage(ConsumerProductModel product) {
		Integer res = MybatisManager.productMapper.updateProductBrokerage(product);
		MybatisManager.commit();
		
		return res;
	}
	
	public static Integer updateUserIDByID(int id, int setUserID) {
		Integer res = MybatisManager.productMapper.updateUserIDByID(id, setUserID);
		MybatisManager.commit();
		
		return res;
	}
	
	public static Integer updateProductNoByID(int id, String productNo) {
		Integer res = MybatisManager.productMapper.updateProductNoByID(id, productNo);
		MybatisManager.commit();
		
		return res;
	}
	
	public static Integer deleteProductByID(int productID) {
		Integer res = MybatisManager.productMapper.deleteProductByID(productID);
		MybatisManager.commit();
		
		return res;
	}
	
	//  查询某天  签约的
	public static List<ConsumerProductModel> selectConsumerProductBySignedTime(String searchDay) {
		List<ConsumerProductModel> productls = MybatisManager.productMapper.selectConsumerProductBySignedTime(searchDay);
	
		return productls;
	}	
	//  查询某天  放款
	public static List<ConsumerProductModel> selectConsumerProductByExtendLoanTime(String searchDay) {
		List<ConsumerProductModel> productls = MybatisManager.productMapper.selectConsumerProductByExtendLoanTime(searchDay);
		
		return productls;
	}
	
	//  查询某天  签约的
	public static List<DayKpiSignedResult> selectConsumerSignedResultBySignedTime(String searchDay) {
		List<DayKpiSignedResult> resultls = MybatisManager.productMapper.selectConsumerSignedResultBySignedTime(searchDay);
		
		return resultls;
	}
	//  查询某天  放款
	public static List<DayKpiSignedResult> selectConsumerResultByExtendLoanTime(String searchDay) {
		List<DayKpiSignedResult> resultls = MybatisManager.productMapper.selectConsumerResultByExtendLoanTime(searchDay);
		
		return resultls;
	}
	
	//  查询  所有 待审批的数量
	public static List<FollowupConsumerResult> selectWaitApprovalNum() {
		List<FollowupConsumerResult> waitNumls = MybatisManager.productMapper.selectWaitApprovalNum();
		
		return waitNumls;
	}
	
	public static ConsumerProductModel selectConsumerProductByProductNo(String productNo) {
		ConsumerProductModel product = MybatisManager.productMapper.selectConsumerProductByProductNo(productNo);
		
		return product;
	}
    
	
	public static void updateProductNoByContract(LoanContractModel contract) {
		int contractID = contract.getId();
		List<ConsumerProductModel> productls = selectProductByContractID(contractID);
		if (productls == null || productls.size() == 0) {
			return;
		}
		String contractNo = contract.getContractNo();
		int cNoLen = contractNo.length();
		for (ConsumerProductModel curProduct : productls) {
			String productNo = curProduct.getProductNo();
			String setProductNo = contractNo + productNo.substring(cNoLen);
			updateProductNoByID(curProduct.getId(), setProductNo);
		}
	}
	
	
}
